package org.test.connectDB.comm;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

/**
 * 分页辅助类 基于EC项目特定条件采用的分页方式
 * 
 * @author tanzy
 * @date 2010-11-1
 */
public class PageHelper {

	private final static Logger logger = Logger.getLogger(PageHelper.class);

	private int pageType = 0;// 分页方式：0、返回整个List 1、返回当页的List
	private int pageShowType = 1;// 分页显示方式：0、默认方式，显示页码 1、不显示页码

	private int currentPage = 1; // 当前页
	private int pageSize = 20; // 每页几条数据
	private int pageShowCount = 5; // 显示的页数

	private int totalPage = 0; // 总页数
	private int totalRows = 0; // 总行数

	private int startRow = 0; // 起始行数
	private int endRow = 0; // 结束行数

	private int startPage = 1; // 显示起始页数
	private int endPage = 1; // 显示结束页数

	private boolean hasNextPage = false;
	private boolean hasPrevPage = false;

	private List dataList;

	// Tag辅助
	private String url; // url
	private String formId; // FormID
	private String zoneId; // ZoneID
	private String iPageName; // 页码名
	private String beanName; // 分页BeanKey
	private String selectPageCss; // 高亮页码
	private String linkType; // 跳转类型名
	private int linkTypeValue; // 跳转类型值
	private String paraName; // 附加参数名
	private String paraValue; // 附加参数值
	public static final String DEFAULT_BEAN_NAME = "PageUtil";
	public static final String DEFAULT_IPAGE = "iPage";
	private boolean showTotalRows = false;// 是否显示总行数


	public PageHelper() {
	}

	public PageHelper(List dataList) {
		this.initPage(dataList);
	}

	/**
	 * 初始化分页数据
	 * 
	 * @param dataList
	 *            列表
	 */
	public void initPage(List dataList) {
		if (dataList == null) {
			dataList = new ArrayList();
		}
		initPage(dataList, dataList.size());
	}

	/**
	 * 初始化分页数据
	 * 
	 * @param dataList
	 *            列表
	 * @param totalRows
	 *            总行数：默认为dataList的大小
	 */
	public void initPage(List dataList, int totalRows) {
		initPage(dataList, totalRows, pageSize);
	}

	/**
	 * 初始化分页数据
	 * 
	 * @param dataList
	 *            列表
	 * @param totalRows
	 *            总行数：默认为dataList的大小
	 * @param pageSize
	 *            每页的大小
	 */
	public void initPage(List dataList, int totalRows, int pageSize) {
		initPage(dataList, totalRows, pageSize, pageShowCount);
	}

	/**
	 * 初始化分页数据
	 * 
	 * @param dataList
	 *            列表
	 * @param totalRows
	 *            总行数：默认为dataList的大小
	 * @param pageSize
	 *            每页的大小
	 * @param pageShowCount
	 *            显示的页码数
	 */
	public void initPage(List dataList, int totalRows, int pageSize,
			int pageShowCount) {
		initPage(dataList, totalRows, pageSize, pageShowCount, pageType);
	}

	/**
	 * 初始化分页数据
	 * 
	 * @param dataList
	 *            列表
	 * @param totalRows
	 *            总行数：默认为dataList的大小
	 * @param pageSize
	 *            每页的大小
	 * @param pageShowCount
	 *            显示的页码数
	 * @param pageType
	 *            分页类型
	 */
	public void initPage(List dataList, int totalRows, int pageSize,
			int pageShowCount, int pageType) {
		this.dataList = dataList;
		this.totalRows = totalRows;
		this.pageSize = pageSize;
		this.pageShowCount = pageShowCount;
		this.pageType = pageType;

		// 计算总页数
		if ((totalRows % pageSize) == 0) {
			this.totalPage = totalRows / pageSize;
		} else {
			this.totalPage = (totalRows / pageSize) + 1;
		}

		// 第一页记录数
		if (totalRows < pageSize) {
			this.endRow = totalRows;
		} else {
			this.endRow = pageSize - 1;
		}

		// 判断是否有下一页
		if (this.currentPage >= this.totalPage) {
			this.hasNextPage = false;
		} else {
			this.hasNextPage = true;
		}

	}

	/**
	 * 取上一页
	 * 
	 * @return
	 */
	public List getPrevPage() {
		if (this.currentPage <= 1) {
			this.currentPage = 1;
		} else {
			this.currentPage = this.currentPage - 1;
		}

		if (this.currentPage >= this.totalPage) {
			this.hasNextPage = false;
		} else {
			this.hasNextPage = true;
		}

		if ((this.currentPage - 1) > 0) {
			this.hasPrevPage = true;
		} else {
			this.hasPrevPage = false;
		}

		return this.getDataList();
	}

	/**
	 * 取下一页
	 * 
	 * @return
	 */
	public List getNextPage() {
		this.currentPage = this.currentPage + 1;
		if (this.currentPage > this.totalPage) {
			this.currentPage = this.totalPage;
		}

		if ((this.currentPage - 1) > 0) {
			this.hasPrevPage = true;
		} else {
			this.hasPrevPage = false;
		}
		if (this.currentPage >= this.totalPage) {
			this.hasNextPage = false;
		} else {
			this.hasNextPage = true;
		}

		return this.getDataList();
	}

	/**
	 * 取首页
	 * 
	 * @return
	 */
	public List getHomePage() {
		this.currentPage = 1;

		if (this.currentPage >= this.totalPage) {
			this.hasNextPage = false;
		} else {
			hasNextPage = true;
		}

		if ((this.currentPage - 1) > 0) {
			this.hasPrevPage = true;
		} else {
			this.hasPrevPage = false;
		}

		return this.getDataList();
	}

	// 尾页
	public List getLastPage() {
		this.currentPage = this.totalPage;

		if (this.currentPage >= this.totalPage) {
			this.hasNextPage = false;
		} else {
			this.hasNextPage = true;
		}

		if ((this.currentPage - 1) > 0) {
			this.hasPrevPage = true;
		} else {
			this.hasPrevPage = false;
		}

		return this.getDataList();
	}

	// 跳转页
	public List getGoToPage(int goToPage) {
		if (goToPage <= 1) {
			return this.getHomePage();
		} else if (goToPage >= this.totalPage) {
			return this.getLastPage();
		} else {
			this.currentPage = goToPage;
			this.hasNextPage = true;
			this.hasPrevPage = true;

			return this.getDataList();
		}
	}

	public List getDataList() {
		return this.getDataList(this.pageShowType);
	}

	public List getDataList(int pageShowType) {
		if (this.dataList == null || this.dataList.isEmpty()) {
			return this.dataList;
		}

		// 开始和结束行数:startRow从0开始
		if (this.currentPage * this.pageSize < this.totalRows) {
			this.endRow = this.currentPage * this.pageSize - 1;
			this.startRow = this.endRow + 1 - this.pageSize;
		} else {
			this.endRow = this.totalRows;
			this.startRow = this.pageSize
					* (this.totalPage > 1 ? (this.totalPage - 1) : 0);
		}

		// 显示页码数
		if (pageShowType == 0) {
			setDisplaySize();
		}

		if (this.pageType == 1) {
			List dataList = new ArrayList(this.endRow - this.startRow + 1);
			for (int i = this.startRow; i < this.endRow; i++) {
				Object dataObj = (Object) this.dataList.get(i);
				dataList.add(dataObj);
			}
			this.dataList = dataList;
		}

		return this.dataList;
	}

	/**
	 * 显示分页页码
	 */
	public void setDisplaySize() {
		int showPage = (this.pageShowCount / 2);
		if (this.totalPage < this.pageShowCount) {
			this.startPage = 1;
			this.endPage = this.totalPage;
		} else {
			if (this.currentPage > showPage) {
				if ((this.currentPage + showPage) < this.totalPage) {
					this.startPage = this.currentPage - showPage;
					this.endPage = this.currentPage + showPage;
				} else {
					this.startPage = this.totalPage - this.pageShowCount + 1;
					this.endPage = this.totalPage;

				}
			} else if ((this.currentPage <= showPage)) {
				this.startPage = 1;
				this.endPage = this.pageShowCount;
			}

		}

	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getPageShowCount() {
		return pageShowCount;
	}

	public void setPageShowCount(int pageShowCount) {
		this.pageShowCount = pageShowCount;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getTotalRows() {
		return totalRows;
	}

	public void setTotalRows(int totalRows) {
		this.totalRows = totalRows;
	}

	public int getStartRow() {
		return startRow;
	}

	public void setStartRow(int startRow) {
		this.startRow = startRow;
	}

	public int getEndRow() {
		return endRow;
	}

	public void setEndRow(int endRow) {
		this.endRow = endRow;
	}

	public int getStartPage() {
		return startPage;
	}

	public void setStartPage(int startPage) {
		this.startPage = startPage;
	}

	public int getEndPage() {
		return endPage;
	}

	public void setEndPage(int endPage) {
		this.endPage = endPage;
	}

	public boolean isHasNextPage() {
		return hasNextPage;
	}

	public void setHasNextPage(boolean hasNextPage) {
		this.hasNextPage = hasNextPage;
	}

	public boolean isHasPrevPage() {
		return hasPrevPage;
	}

	public void setHasPrevPage(boolean hasPrevPage) {
		this.hasPrevPage = hasPrevPage;
	}

	public int getPageType() {
		return pageType;
	}

	public void setPageType(int pageType) {
		this.pageType = pageType;
	}

	public int getPageShowType() {
		return pageShowType;
	}

	public void setPageShowType(int pageShowType) {
		this.pageShowType = pageShowType;
	}

	public void setDataList(List dataList) {
		this.dataList = dataList;
	}

	public String getIPageName() {
		return iPageName;
	}

	public void setIPageName(String iPageName) {
		this.iPageName = iPageName;
	}

	public String getLinkType() {
		return linkType;
	}

	public void setLinkType(String linkType) {
		this.linkType = linkType;
	}

	public String getParaName() {
		return paraName;
	}

	public void setParaName(String paraName) {
		this.paraName = paraName;
	}

	public String getSelectPageCss() {
		return selectPageCss;
	}

	public void setSelectPageCss(String selectPageCss) {
		this.selectPageCss = selectPageCss;
	}

	public String getFormId() {
		return formId;
	}

	public void setFormId(String formId) {
		this.formId = formId;
	}

	public String getZoneId() {
		return zoneId;
	}

	public void setZoneId(String zoneId) {
		this.zoneId = zoneId;
	}

	public int getLinkTypeValue() {
		return linkTypeValue;
	}

	public void setLinkTypeValue(int linkTypeValue) {
		this.linkTypeValue = linkTypeValue;
	}

	public String getParaValue() {
		return paraValue;
	}

	public void setParaValue(String paraValue) {
		this.paraValue = paraValue;
	}

	public String getBeanName() {
		return beanName;
	}

	public void setBeanName(String beanName) {
		this.beanName = beanName;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public boolean isShowTotalRows() {
		return showTotalRows;
	}

	public void setShowTotalRows(boolean showTotalRows) {
		this.showTotalRows = showTotalRows;
	}

}